using ArticleManagementSystem.Api.Db;
using ArticleManagementSystem.Api.Entity;
using ArticleManagementSystem.Api.Params;
using ArticleManagementSystem.Api.Repostisory;
using Microsoft.AspNetCore.Mvc;
using ArticleManagementSystem.Api.Utils;
using Microsoft.EntityFrameworkCore;
using System.Collections;
using System.Dynamic;
using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;

namespace ArticleManagementSystem.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class ArticleModificationController : ControllerBase
    {
        private readonly DataBase _context;
        private readonly IRepostisory<Carousel> _carouselRepostisory;

        public ArticleModificationController(IRepostisory<Carousel> carouselRepostisory, IRepostisory<Article> articleRepostisory, IRepostisory<Imgs> imgRepostisory, IWebHostEnvironment iWebHostEnvironment)
        {
            _context = new DataBase();
            _carouselRepostisory = carouselRepostisory;
        }
        //获取文章
        [HttpGet, Route("GetArticle")]
        public dynamic GetArticle()
        {
            var articles = _context.article.ToList();
            var imgs = _context.imgs.ToList();
            var users = _context.users.ToList();
            var carousels = _context.carousel.ToList();

            var list = new List<dynamic>();
            foreach (var item in articles)
            {
                var img = imgs.Where(x => x.Id == item.ImgId).FirstOrDefault();
                var user = users.Where(x => x.Id == item.UserId).FirstOrDefault();
                var carousel = carousels.Where(x => x.ArticleId == item.Id).FirstOrDefault();


                var tmp = new
                {
                    Id = item.Id,
                    item.Title,
                    item.Content,
                    item.ArticleDetails,
                    item.UserId,
                    item.ArticleType,
                    item.CreatedTime,
                    ImgPath = img != null ? img.ImgsPath : "",
                    ImgId = img != null ? img.Id.ToString() : "",
                    Username = user != null ? user.Username : "",
                    iscarousel = carousel != null ? true : false
                };
                list.Add(tmp);
            }

            // ArrayList arraylist = new ArrayList();
            // foreach (var item in table)
            // {

            //       var Articleid = item.Id;

            //     arraylist.Add(Articleid);
            // }
            // var restable = ArticleSelectHelper.GetArticleMsg(arraylist,_context);

            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = list,
                Msg = "获取成功"
            });

        }
        //修改轮播图
        [HttpPost, Route("EditCarousel")]
        public dynamic EditCarousel(EditCarousel carousel)
        {
            var ImgId = carousel.ImgId;
            var ArticleId = carousel.ArticleId;
            var AddOrRemove = _carouselRepostisory.Table.Where(x => x.ArticleId == ArticleId).FirstOrDefault();
            if (AddOrRemove == null)
            {
                var res = new Carousel
                {
                    ImgId = ImgId,
                    ArticleId = ArticleId
                };

                _carouselRepostisory.Insert(res);
                return new
                {
                    code = 200,
                    msg = "修改成功"
                };
            }
            else
            {
                var CarouselId = AddOrRemove.Id;
                _carouselRepostisory.Deleted(CarouselId);
                return new
                {
                    code = 200,
                    msg = "修改成功"
                };
            }


        }
    }
}